package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SysLogMapper;
import com.itheima.pojo.ExceptionLog;
import com.itheima.pojo.SysLog;
import com.itheima.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Map;

@Service
public class SysLogServiceImpl implements SysLogService {

    @Autowired
    private SysLogMapper sysLogMapper;

    @Override
    public Page<SysLog> getLogList(Map<String, Object> searchMap) {
        //获取当前页
        Integer currentPage = (Integer) searchMap.get("currentPage");
        if(null == currentPage || currentPage < 0){
            currentPage = 0;
        }
        //设置默认每页显示条数
        Integer pageSize = (Integer) searchMap.get("pageSize");
        if (null == pageSize || pageSize <= 0) {
            pageSize = 10;
        }
        PageHelper.startPage(currentPage,pageSize);
        Example example = this.createExample(searchMap);
        Page<SysLog> pageList = (Page<SysLog>) sysLogMapper.selectByExample(example);
        return pageList;
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(ExceptionLog.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            if(searchMap.get("uid")!=null && !"".equals(searchMap.get("uid"))){
                criteria.andLike("uid","%"+searchMap.get("uid")+"%");
            }
            if(searchMap.get("userName")!=null && !"".equals(searchMap.get("userName"))){
                criteria.andLike("userName","%"+searchMap.get("userName")+"%");
            }
            if(searchMap.get("adminUid")!=null && !"".equals(searchMap.get("adminUid"))){
                criteria.andLike("adminUid","%"+searchMap.get("adminUid")+"%");
            }
            if(searchMap.get("ip")!=null && !"".equals(searchMap.get("ip"))){
                criteria.andLike("ip","%"+searchMap.get("ip")+"%");
            }
            if(searchMap.get("url")!=null && !"".equals(searchMap.get("url"))){
                criteria.andLike("url","%"+searchMap.get("url")+"%");
            }
            if(searchMap.get("type")!=null && !"".equals(searchMap.get("type"))){
                criteria.andLike("type","%"+searchMap.get("type")+"%");
            }
            if(searchMap.get("classPath")!=null && !"".equals(searchMap.get("classPath"))){
                criteria.andLike("classPath","%"+searchMap.get("classPath")+"%");
            }
            if(searchMap.get("method")!=null && !"".equals(searchMap.get("method"))){
                criteria.andLike("method","%"+searchMap.get("method")+"%");
            }
            if(searchMap.get("params")!=null && !"".equals(searchMap.get("params"))){
                criteria.andLike("params","%"+searchMap.get("params")+"%");
            }
            if(searchMap.get("operation")!=null && !"".equals(searchMap.get("operation"))){
                criteria.andLike("operation","%"+searchMap.get("operation")+"%");
            }
            if(searchMap.get("ipSource")!=null && !"".equals(searchMap.get("ipSource"))){
                criteria.andLike("ipSource","%"+searchMap.get("ipSource")+"%");
            }
            if(searchMap.get("spendTime")!=null){
                criteria.andEqualTo("spendTime",searchMap.get("spendTime"));
            }
            if(searchMap.get("status")!=null){
                criteria.andEqualTo("status",searchMap.get("status"));
            }
            if (searchMap.get("startTime")!=null){
                criteria.andGreaterThan("createTime",searchMap.get("startTime"));
            }
            if (searchMap.get("keyword")!=null){
                criteria.andLessThanOrEqualTo("createTime",searchMap.get("endTime"));
            }


        }
        return example;
    }
}
